home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Camelot / Camelot 064 (1990-02)(Swedish User Group of Amiga)(SE)(PD)[WB].zip / Camelot 064 (1990-02)(Swedish User Group of Amiga)(SE)(PD)[WB].adf / cclib / source / loc.c < prev    next >
C/C++ Source or Header  |  1990-02-14  |  4KB  |  184 lines

  1. /*-------------------------------------------------------------------------+
  2.  |                                       |
  3.  | Name:    loc                                |
  4.  | Purpose: counts lines of actual code and comments and prints stats       |
  5.  |                                       |
  6.  | Author:  Robert W. Albrecht               Date: 9/89           |
  7.  +-------------------------------------------------------------------------*/
  8.  
  9. #include "stdio.h"
  10.  
  11. long whitechars, codechars, commentchars, codelines;
  12. long whitechars_total, codechars_total, commentchars_total, codelines_total;
  13. long blanklines, blanklines_total, filecount;
  14. short quiet;
  15. short _math = 0; /* <= don't load MathIeeeDoubBas.library */
  16.  
  17. void do_totals() /* add up total stats */
  18. {
  19. whitechars_total += whitechars;
  20. codechars_total += codechars;
  21. commentchars_total += commentchars;
  22. codelines_total += codelines;
  23. blanklines_total += blanklines;
  24. blanklines = whitechars = codechars = commentchars = codelines = 0L;
  25. }
  26.  
  27. void printtotals() /* print total stats */
  28. {
  29. long avglines;
  30. void printstats();
  31.  
  32. avglines = codelines_total/filecount;
  33.  
  34. printf("\nTotal statistics for %ld files...\n",filecount);
  35. whitechars = whitechars_total;
  36. codechars = codechars_total;
  37. commentchars = commentchars_total;
  38. codelines = codelines_total;
  39. blanklines = blanklines_total;
  40. printstats();
  41. printf("The average number of code lines per file is %ld.\n\n",avglines);
  42. }
  43.  
  44. void printstats() /* print stats */
  45. {
  46. long avgline;
  47. long code_p;
  48. long code_pn;
  49.  
  50. if( codelines )
  51.    avgline = codechars/codelines;
  52. else
  53.    avgline = 0;
  54. code_pn = code_p = codechars*100;
  55. if( commentchars + whitechars + codechars )
  56.    code_p /= (commentchars+whitechars+codechars);
  57. else
  58.    code_p = 0;
  59. if( whitechars + codechars )
  60.    code_pn /= (whitechars+codechars);
  61. else
  62.    code_pn = 0;
  63.  
  64. printf("%ld lines of code, ",codelines);
  65. printf("%ld blank lines, ",blanklines);
  66. printf("%ld bytes of code,\n",codechars);
  67. printf("%ld bytes of whitespace, ",whitechars);
  68. printf("and %ld bytes of comments.\n",commentchars);
  69. printf("The average code line has %ld bytes.\n",avgline);
  70. printf("The code content is %ld%%, %ld%% without comments.\n",
  71.    code_p, code_pn);
  72. }
  73.  
  74. #define COMMENT 1
  75. #define BLANK 1
  76. #define CODE 0
  77.  
  78. void loc(fname)  /* counts lines of actual code */
  79. char *fname;
  80. {
  81. register FILE *f;
  82. register short c, oldc;
  83. register short mode, hascode;
  84.  
  85. if( f = fopen(fname,"r") )
  86.    {
  87.    c = oldc = -1;
  88.    mode = CODE;
  89.    hascode = BLANK;
  90.  
  91.    while( (c = getc(f)) != EOF )
  92.       {
  93.       switch( mode )
  94.      {
  95.      case CODE:
  96.         {
  97.         if( c == '*' && oldc == '/' )
  98.            {
  99.            mode = COMMENT;
  100.            codechars--;
  101.            }
  102.         else switch(c)
  103.            {
  104.            case '\n':
  105.           if( hascode == CODE )
  106.              {
  107.              codelines++;
  108.              hascode = BLANK;
  109.              }
  110.           else
  111.              blanklines++;
  112.            case '\t': case '\v': case '\b':
  113.            case '\r': case '\f': case '\a':
  114.            case ' ':
  115.           whitechars++;
  116.            break;
  117.  
  118.            default:
  119.           hascode = CODE;
  120.           codechars++;
  121.            break;
  122.            }
  123.         }
  124.      break;
  125.  
  126.      case COMMENT:
  127.         if( c == '/' && oldc == '*' )
  128.            mode = CODE;
  129.         else
  130.            commentchars++;
  131.      break;
  132.      }
  133.       oldc = c;
  134.       }
  135.    if( !quiet )
  136.       {
  137.       printf("\nThe file %s contains...\n",fname);
  138.       printstats();
  139.       }
  140.    do_totals();
  141.    filecount++;
  142.    fclose(f);
  143.    }
  144. else
  145.    printf("Can't open input file %s\n",fname);
  146. }
  147.  
  148.  
  149.  
  150. void main(argc,argv) /* main routine */
  151. short argc;
  152. char *argv[];
  153. {
  154. short i;
  155. char *ptr, *scdir();
  156.  
  157. if( argc > 1 )
  158.    {
  159.    printf("Lines Of C (LOC) by Robert W. Albrecht\n\n");
  160.  
  161.    for( i = 1; i < argc; i++)
  162.       {
  163.       if( argv[i][0] == '-' )
  164.      {
  165.      if( argv[i][1] == 'Q' || argv[i][1] == 'q' )
  166.         quiet = 1;
  167.      }
  168.       else
  169.      {
  170.      while( ptr = scdir(argv[i]) )
  171.         loc(ptr);
  172.      }
  173.       }
  174.    if( filecount > 1 || quiet )
  175.       printtotals();
  176.    }
  177. else if( argc == 1 )
  178.    {
  179.    printf("Lines Of C (LOC) by Robert W. Albrecht\n\n");
  180.    printf("SYNTAX: loc [-Q] file1 file2 ...\n");
  181.    printf("Wild-Card characters accepted.\n");
  182.    }
  183. }
  184.